package com.baichang.taoshunda.controller.mobile.business;

import com.baichang.framework.controller.BaseMobileController;
import com.baichang.framework.controller.IService;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.controller.ControllerConstants;
import com.baichang.taoshunda.service.BackgroundImageService;
import com.baichang.taoshunda.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by ${wushenglu} on 2018/3/12.
 */
@Controller
@RequestMapping(ControllerConstants.BACKGROUND)
public class BackgroundImageController extends BaseMobileController {
    @Autowired
    private BackgroundImageService backgroundImageService;
    @Autowired
    private UserService userService;

    /**
     * 添加背景图
     *
     * @param request
     * @param response
     */
    @RequestMapping("/addBackGroundImage")
    public void addBackGroundImage(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer userId = getInteger(json, "userId", 0);
                String backGroundImage = getString(json, "backGroundImage", "");
                return backgroundImageService.addBackGroundImage(userId, backGroundImage);
            }
        });
    }

    /**
     * 设置背景图
     *
     * @param request
     * @param response
     */
    @RequestMapping("/installBackGroundImage")
    public void installBackGroundImage(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer userId = getInteger(json, "userId", 0);
                String backGroundImage = getString(json, "backGroundImage", "");
                return userService.installBackGroundImage(userId, backGroundImage);
            }
        });
    }

    /**
     * 查看素材库
     *
     * @param request
     * @param response
     */
    @RequestMapping("/findBackGroundImageList")
    public void findBackGroundImageList(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer userId = getInteger(json, "userId", 0);
                Integer nowPage = getInteger(json, "nowPage", 1);
                Integer pageSize = getInteger(json, "pageSize", 20);
                return backgroundImageService.findBackGroundImageList(userId, nowPage, pageSize);
            }
        });
    }

}
